package com.pocketsweet.ui;

import java.util.Collections;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.pocketsweet.MLCache;
import com.pocketsweet.R;
import com.pocketsweet.MLApplication;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.chatui.domain.User;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onLeftContainerListener;

@ContentView(R.layout.activity_blacklist)
public class BlackList extends BaseActivity {

	@ViewInject(R.id.blackListView)
	private ListView blackListView;

	@ViewInject(R.id.mainHeader)
	private HeaderLayout header;

	private BlacklistAdapater adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		setHeaderFunction();
		getblack();
	}

	private void getblack() {
		// 从本地获取黑名单
		List<String> blacklist = EMContactManager.getInstance()
				.getBlackListUsernames();

		// 显示黑名单列表
		if (blacklist != null) {
			Collections.sort(blacklist);
			adapter = new BlacklistAdapater(this, 1, blacklist);
			blackListView.setAdapter(adapter);
		}

	}

	public void setHeaderFunction() {
		header = (HeaderLayout) findViewById(R.id.mainHeader);
		header.init(HeaderStyle.IMG_TITLE_TEXT);
		header.setMiddleText("黑名单");
		header.setRightContentShow(View.GONE);
		header.setLeftContainerClickListener(new onLeftContainerListener() {

			@Override
			public void onClick() {
				BlackList.this.finish();
			}
		});
	}

	/**
	 * 移出黑民单
	 * 
	 * @param tobeRemoveUser
	 */
	void removeOutBlacklist(final String tobeRemoveUser) {
		final ProgressDialog pd = new ProgressDialog(this);
		pd.setMessage(getString(R.string.be_removing));
		pd.setCanceledOnTouchOutside(false);
		pd.show();
		new Thread(new Runnable() {
			public void run() {
				try {
					// 移出黑民单
					EMContactManager.getInstance().deleteUserFromBlackList(
							tobeRemoveUser);
					runOnUiThread(new Runnable() {
						public void run() {
							pd.dismiss();
							adapter.remove(tobeRemoveUser);
						}
					});
				} catch (EaseMobException e) {
					e.printStackTrace();
					runOnUiThread(new Runnable() {
						public void run() {
							pd.dismiss();
							Toast.makeText(getApplicationContext(),
									R.string.Removed_from_the_failure, 0)
									.show();
						}
					});
				}
			}
		}).start();
	}

	/**
	 * adapter
	 * 
	 */
	private class BlacklistAdapater extends ArrayAdapter<String> {

		public BlacklistAdapater(Context context, int textViewResourceId,
				List<String> objects) {
			super(context, textViewResourceId, objects);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = View.inflate(getContext(), R.layout.item_black,
						null);
			}

			final String userId = getItem(position);

			// 设置姓名
			TextView name = (TextView) convertView
					.findViewById(R.id.tvuserName);
			name.setText(MLCache.getNickById(userId));
			// 设置头像
			ImageView avatar = (ImageView) convertView
					.findViewById(R.id.imgAvatar);
			UserService.displaySquare(MLCache.getAvatarById(userId), avatar);

			// 移除时间
			TextView remove = (TextView) convertView
					.findViewById(R.id.tvRemove);
			remove.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					removeOutBlacklist(userId);
				}
			});

			return convertView;
		}

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}
}
